

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.test.standard_scaler_test &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.test.standard_scaler_test</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.test.standard_scaler_test</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">unittest</span>

<span class="kn">from</span> <span class="nn">sklearn.preprocessing</span> <span class="k">import</span> <span class="n">StandardScaler</span> <span class="k">as</span> <span class="n">SSL</span>

<span class="kn">from</span> <span class="nn">arch.api</span> <span class="k">import</span> <span class="n">eggroll</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.instance</span> <span class="k">import</span> <span class="n">Instance</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.standard_scaler</span> <span class="k">import</span> <span class="n">StandardScaler</span>


<div class="viewcode-block" id="TestStandardScaler"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler">[docs]</a><span class="k">class</span> <span class="nc">TestStandardScaler</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<div class="viewcode-block" id="TestStandardScaler.setUp"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.setUp">[docs]</a>    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">test_data</span> <span class="o">=</span> <span class="p">[</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
            <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
            <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span>
            <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">],</span>
            <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="o">-</span><span class="mi">100</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">7</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mf">9.0</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span>
            <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">10.0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">]</span>
        <span class="p">]</span>
        <span class="n">str_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">%H%M%S&quot;</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">())</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">test_instance</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">td</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">test_instance</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Instance</span><span class="p">(</span><span class="n">features</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">td</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data_to_eggroll_table</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_instance</span><span class="p">,</span> <span class="n">str_time</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="o">.</span><span class="n">schema</span><span class="p">[</span><span class="s1">&#39;header&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;fid&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]))]</span></div>

<div class="viewcode-block" id="TestStandardScaler.print_table"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.print_table">[docs]</a>    <span class="k">def</span> <span class="nf">print_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">collect</span><span class="p">())):</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="p">)</span></div>

<div class="viewcode-block" id="TestStandardScaler.data_to_eggroll_table"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.data_to_eggroll_table">[docs]</a>    <span class="k">def</span> <span class="nf">data_to_eggroll_table</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">jobid</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">work_mode</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="n">eggroll</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="n">jobid</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="n">work_mode</span><span class="p">)</span>
        <span class="n">data_table</span> <span class="o">=</span> <span class="n">eggroll</span><span class="o">.</span><span class="n">parallelize</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">include_key</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data_table</span></div>

<div class="viewcode-block" id="TestStandardScaler.get_table_instance_feature"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.get_table_instance_feature">[docs]</a>    <span class="k">def</span> <span class="nf">get_table_instance_feature</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table_instance</span><span class="p">):</span>
        <span class="n">res_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">table_instance</span><span class="o">.</span><span class="n">collect</span><span class="p">()):</span>
            <span class="n">res_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">features</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>

        <span class="k">return</span> <span class="n">res_list</span></div>

    <span class="c1"># test with (with_mean=True, with_std=True):</span>
<div class="viewcode-block" id="TestStandardScaler.test_fit1"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_fit1">[docs]</a>    <span class="k">def</span> <span class="nf">test_fit1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>

        <span class="n">scaler</span> <span class="o">=</span> <span class="n">SSL</span><span class="p">()</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">),</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">mean_</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">std</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">scale_</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span></div>

    <span class="c1"># test with (with_mean=False, with_std=True):</span>
<div class="viewcode-block" id="TestStandardScaler.test_fit2"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_fit2">[docs]</a>    <span class="k">def</span> <span class="nf">test_fit2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>

        <span class="n">scaler</span> <span class="o">=</span> <span class="n">SSL</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">),</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">mean</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">std</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">scale_</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span></div>

    <span class="c1"># test with (with_mean=True, with_std=False):</span>
<div class="viewcode-block" id="TestStandardScaler.test_fit3"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_fit3">[docs]</a>    <span class="k">def</span> <span class="nf">test_fit3</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>

        <span class="n">scaler</span> <span class="o">=</span> <span class="n">SSL</span><span class="p">(</span><span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">),</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">mean_</span><span class="p">,</span> <span class="mi">4</span><span class="p">)))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">std</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span> <span class="p">[</span><span class="mi">1</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="n">std</span><span class="p">])</span></div>

    <span class="c1"># test with (with_mean=False, with_std=False):</span>
<div class="viewcode-block" id="TestStandardScaler.test_fit4"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_fit4">[docs]</a>    <span class="k">def</span> <span class="nf">test_fit4</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>

        <span class="n">scaler</span> <span class="o">=</span> <span class="n">SSL</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">),</span>
                             <span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]))])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">std</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">test_data</span><span class="p">[</span><span class="mi">0</span><span class="p">]))])</span></div>

    <span class="c1"># test with (with_mean=True, with_std=True):</span>
<div class="viewcode-block" id="TestStandardScaler.test_transform1"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_transform1">[docs]</a>    <span class="k">def</span> <span class="nf">test_transform1</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>
        <span class="n">transform_data</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">transform_data</span><span class="p">),</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">))</span></div>

    <span class="c1"># test with (with_mean=True, with_std=False):</span>
<div class="viewcode-block" id="TestStandardScaler.test_transform2"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_transform2">[docs]</a>    <span class="k">def</span> <span class="nf">test_transform2</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>
        <span class="n">transform_data</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">transform_data</span><span class="p">),</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">))</span></div>

    <span class="c1"># test with (with_mean=False, with_std=True):</span>
<div class="viewcode-block" id="TestStandardScaler.test_transform3"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_transform3">[docs]</a>    <span class="k">def</span> <span class="nf">test_transform3</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>
        <span class="n">transform_data</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">transform_data</span><span class="p">),</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">))</span></div>

    <span class="c1"># test with (with_mean=False, with_std=False):</span>
<div class="viewcode-block" id="TestStandardScaler.test_transform4"><a class="viewcode-back" href="../../../../federatedml.feature.test.html#federatedml.feature.test.standard_scaler_test.TestStandardScaler.test_transform4">[docs]</a>    <span class="k">def</span> <span class="nf">test_transform4</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">standard_scaler</span> <span class="o">=</span> <span class="n">StandardScaler</span><span class="p">(</span><span class="n">with_mean</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">with_std</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">fit_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">)</span>
        <span class="n">transform_data</span> <span class="o">=</span> <span class="n">standard_scaler</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table_instance</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">std</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertListEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">transform_data</span><span class="p">),</span>
                             <span class="bp">self</span><span class="o">.</span><span class="n">get_table_instance_feature</span><span class="p">(</span><span class="n">fit_instance</span><span class="p">))</span></div></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>